package com.utils;

import android.webkit.URLUtil;
import org.w3c.dom.*;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Objects;

public class ParseXmlUtil
{
	static Apkinfo apkinfo=new Apkinfo();
	public  static Apkinfo parseXml()
	{

		try {
			URL url = new URL("");
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.connect();
			InputStream stream = connection.getInputStream();
			HashMap<String, String> hashMap = new HashMap<String, String>();
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.parse(stream);
			Element root = document.getDocumentElement();
			NodeList childNodes = root.getChildNodes();
			for (int j = 0; j < childNodes.getLength(); j++)
			{
				Node childNode = (Node) childNodes.item(j);
				if (childNode.getNodeType() == Node.ELEMENT_NODE)
				{
					Element childElement = (Element) childNode;
					if ("version".equals(childElement.getNodeName()))
					{
						//hashMap.put("version",childElement.getFirstChild().getNodeValue());
						apkinfo.setVersion(Integer.parseInt(childElement.getFirstChild().getNodeValue()));
					}
					else if (("minversion".equals(childElement.getNodeName())))
					{
						//hashMap.put("minversion", childElement.getFirstChild().getNodeValue());
						apkinfo.setMinversion(Integer.parseInt(childElement.getFirstChild().getNodeValue()));
					} else if (("name".equals(childElement.getNodeName())))
					{
						//hashMap.put("name",childElement.getFirstChild().getNodeValue());
						apkinfo.setName(childElement.getFirstChild().getNodeValue());
					}
					else if (("url".equals(childElement.getNodeName())))
					{
						//hashMap.put("url",childElement.getFirstChild().getNodeValue());
						apkinfo.setUrl(childElement.getFirstChild().getNodeValue());
						//apkinfo.setFile(new File(childElement.getFirstChild().getNodeValue()));
					}
					else if (("updatedesc".equals(childElement.getNodeName())))
					{
						//hashMap.put("updatedesc",childElement.getFirstChild().getNodeValue());
						apkinfo.setUpdatedesc(childElement.getFirstChild().getNodeValue());
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (DOMException e) {
			e.printStackTrace();
		} catch (Exception e){
			e.printStackTrace();
		}
		return apkinfo;
	}

	public static class Apkinfo {
		private int  version;//>28</version>
		private int  minversion=0;//>28</minversion>
		private String  name;//>hymars_2.4.0.apk</name>
		private String  url;//>http://www.hicash.cn/download/hymars_2.4.0.apk</url>
		private String  updatedesc;//>1、上线嗨车贷。</updatedesc>


		public int getVersion() {
			return version;
		}

		public void setVersion(int version) {
			this.version = version;
		}

		public int getMinversion() {
			return minversion;
		}

		public void setMinversion(int minversion) {
			this.minversion = minversion;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getUrl() {
			return url;
		}

		public void setUrl(String url) {
			this.url = url;
		}

		public String getUpdatedesc() {
			return updatedesc;
		}

		public void setUpdatedesc(String updatedesc) {
			this.updatedesc = updatedesc;
		}
	}

}
